package activity.bluetooth;

import java.util.ArrayList;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.util.Log;
public class ContactManager {
	
	ContentProvider cp;
	ContentResolver cr;
	
	public ContactManager(ContentResolver cr){
	
//		ContactsContract.Contacts.CONTENT_URI;
		this.cr = cr;
		
	}
	
	// Only a method for testing. Cant do much before they update the API level on the computers.
	public void loopContacts(){
		
		String[] projection ={ People._ID, People.NAME, People.NUMBER,People.PRIMARY_EMAIL_ID};
		
		Uri contacts = Uri.parse("content://contacts/people");
		
		Cursor people = this.cr.query(contacts, projection, null, null, null);

		ArrayList<String> ids = new ArrayList<String>();
		while(people.moveToNext()) {
			
			String id = people.getString(people.getColumnIndex(People._ID));
			ids.add(id);
			String name = people.getString(people.getColumnIndex(People.NAME));
		 
		 Log.i("contact","ID: "+id);
		 Log.i("contact","Name: "+name);
		}
		
		people.close();

		String[] ppl = new String[ids.size()];
		for(int i = 0; i<ppl.length;i++){
			ppl[i] = ids.get(i);
		}
		
		Cursor emailCur = this.cr.query( 
				Contacts.ContactMethods.CONTENT_EMAIL_URI, 
				null,
				Contacts.ContactMethods.PERSON_ID + " = ?", 
				ppl, null); 
		while (emailCur.moveToNext()) { 
			String email = emailCur.getString(emailCur.getColumnIndex(People.IM_ACCOUNT));
		    // This would allow you get several email addresses
			Log.i("contact","email: "+email);
		} 
		emailCur.close();


	}
	

}
